package com.design.twords.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.design.twords.R;
import com.design.twords.config.ConfigData;
import com.design.twords.database.FeedBack;
import com.design.twords.utils.MyApplication;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Create by Alen
 * Time:  23:04
 */
public class FeekBackFragment extends Fragment {

    private static final String TAG = "FeekBackFragment";

    private EditText type, describe;
    private Button send;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.item_feedback, container, false);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MyApplication.getInstance().addActivity(getActivity());
        initView();

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FeedBack feedBack = new FeedBack();
                feedBack.setUserId(ConfigData.getUserId());
                feedBack.setType(type.getText().toString());
                feedBack.setMsg(describe.getText().toString());
                feedBack.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if (e == null) {
                            Snackbar.make(view, "感谢您的反馈", Snackbar.LENGTH_LONG).show();
                            type.setText("");
                            describe.setText("");
                        } else {
                            Log.d(TAG, "error " + e.toString());
                            Snackbar.make(view, "发送失败,请检查网络", Snackbar.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });
    }

    private void initView() {
        type = getActivity().findViewById(R.id.feedback_type);
        describe = getActivity().findViewById(R.id.feedback_describe);
        send = getActivity().findViewById(R.id.feedback_send);
    }

    @Override
    public void onStart() {
        super.onStart();
    }
}
